from os import listdir
import boundaryFinder as bf
import cv2
import fnmatch

from boundingBox import getBoundedBox
def inIncisorBox(image):
    (left, right, top, bottom) = bf.getIncisorBoundary(image)
    if(left > bf.left() and right < bf.right() and top > bf.top() and bottom < bf.bottom()):
        return True
    return False

def getReferenceBoundedBoxes():
    for filename in fnmatch.filter(listdir("Panoramic images/segmented"),'*.png'):
        referenceImage = cv2.imread("Panoramic images/segmented/"+filename)
        referenceImage = cv2.cvtColor(referenceImage, cv2.COLOR_BGR2GRAY)
        referenceBoundingBox = getBoundedBox(referenceImage)
        cv2.imwrite("Reference boxes/"+filename, referenceBoundingBox)
        
def hammingDistance(sampleTeeth, referenceTeeth):
    if sampleTeeth.shape[0] > referenceTeeth.shape[0]:
        referenceTeeth = cv2.resize(referenceTeeth, (sampleTeeth.shape[0],sampleTeeth.shape[1]))
    else:
        sampleTeeth = cv2.resize(sampleTeeth, (referenceTeeth.shape[0],referenceTeeth.shape[1]))
    hamming = 0
    size = 0
    for rowIndex,row in enumerate(sampleTeeth):
        for columnIndex,value in enumerate(row):
            if value > 0 or referenceTeeth[rowIndex,columnIndex] > 0:
                size += 1
            if (value > 0 and referenceTeeth[rowIndex,columnIndex] == 0) or (value == 0 and referenceTeeth[rowIndex,columnIndex] > 0):
                hamming += 1
            
    return float(hamming)/float(size)
        
    